//
//  PoperPresentationController.swift
//  SwiftWeiBo
//
//  Created by wyw on 16/9/18.
//  Copyright © 2016年 Style_Y. All rights reserved.
//

import UIKit

class PoperPresentationController: UIPresentationController {
    
    var presentFrame = CGRect.zero
    
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
    }
    
    /// 即将布局转场子视图时调用
    override func containerViewWillLayoutSubviews() {
        
        if presentFrame == CGRect.zero {
            // 1.修改弹出视图的大小
            presentedView?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        } else {
            presentedView?.frame = presentFrame
        }
        
        // 2.在容器视图上添加一个蒙版，插入到展现视图的下面
        containerView?.insertSubview(converView, at: 0)
    }
    
    // MARK： - 懒加载
    private lazy var converView : UIView = {
        // 1. 创建蒙版view
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        view.frame = UIScreen.main.bounds
        
        // 2.添加手势事件
        let tap = UITapGestureRecognizer(target: self, action: #selector(PoperPresentationController.close))
        view.addGestureRecognizer(tap)
        return view
        
    }()
    
    func close() {
        print(#function)
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}
